package hk.m4s.lr.repair.test.ui.baoyang;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.common.zbar.CaptureActivity;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModel;
import hk.m4s.lr.repair.test.model.MaintenanceInfoModelResult;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.ui.adapter.BaoYangListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.DeviceMainListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.LookMainListAdapter;
import hk.m4s.lr.repair.test.views.InnerGridView;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/22.
 * 保养任务详情
 */

public class LookBaoYangActivity extends UeBaseActivity implements VListView.IXListViewListener{
    private Context context;
    private TextView equ_titlte,tipsText;
    private InnerGridView innerGridView;
    private String type="",state="",name="";
    private List<MaintenanceInfoModel> lists = new ArrayList<>();
    private LookMainListAdapter deviceMainListAdapter;
    private VListView vlist;
    private int pageNum = 0;
    private int pageSize = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_look_baoyang_detaile);
        context=this;
        hiddenFooter();
        showGoBackBtn();
        name=getIntent().getStringExtra("name");
        if(name!=null){
            setTitleText(name);
        }else{
            name="保养任务";
            setTitleText("保养任务");
        }
        type=getIntent().getStringExtra("type");
        state=getIntent().getStringExtra("state");
        if(state.equals("-1")){
            getSqcan().setVisibility(View.VISIBLE);
        }else{
            getSqcan().setVisibility(View.GONE);
        }
        getSqcan().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LookBaoYangActivity.this, CaptureActivity.class).putExtra("type","6").putExtra("intents",type));
                finish();
            }
        });
        findBaseView();
    }
    private void findBaseView() {
        equ_titlte=(TextView)findViewById(R.id.equ_titlte);
        tipsText=(TextView)findViewById(R.id.tipsText);
        innerGridView=(InnerGridView)findViewById(R.id.innergrid);
        vlist=(VListView)findViewById(R.id.vlist);
        vlist.setXListViewListener(this);
        deviceMainListAdapter = new LookMainListAdapter(context, lists,name);
        vlist.setAdapter(deviceMainListAdapter);

    }
    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    protected void onResume() {
        super.onResume();
        deviceMaintenanceInfo("1");
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub

        vlist.setPullLoadEnable(true);
        pageNum=0;
        deviceMaintenanceInfo("1");
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(true);
        pageNum++;
        deviceMaintenanceInfo("2");
    }
    public void deviceMaintenanceInfo(final String refreshType) {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("type", type);
            jsondata.put("state", state);
            parmas.put("jsonText", jsondata.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DeviceListervice.taskByState(context, parmas, new ResponseCallback<MaintenanceInfoModelResult>() {
            @Override
            public void onSuccess(MaintenanceInfoModelResult accountModel) {
                onLoad();
                if(accountModel.getList()!=null) {
                    tipsText.setVisibility(View.GONE);
                    switch (refreshType) {
                        case "1":
                            lists.clear();
                            lists.addAll(accountModel.getList());
                            if (accountModel.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            } else {
                                vlist.showFooterView();
                                vlist.setPullLoadEnable(true);
                            }
                            break;
                        case "2":
                            lists.addAll(accountModel.getList());
                            if (accountModel.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            }
                            break;
                    }
                    deviceMainListAdapter.notifyDataSetChanged();
                }else{
                    tipsText.setVisibility(View.VISIBLE);
                    vlist.removeFooterView();
                    vlist.setPullLoadEnable(false);
                }
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}
